package com.luyou.awt.event;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 监听器监听多个组件
 */
public class MultiListener {

    private Frame frame = new Frame("测试");
    private TextArea ta = new TextArea(6, 40);
    private Button b1 = new Button("按钮一");
    private Button b2 = new Button("按钮二");

    public void init(){
       FirstListener fl = new FirstListener();
       b1.addActionListener(fl);
       b1.addActionListener(new SecondListener());

       b2.addActionListener(fl);

       frame.add(ta);

       Panel p = new Panel();
       p.add(b1);
       p.add(b2);
       frame.add(p, BorderLayout.SOUTH);
       frame.pack();
       frame.setVisible(true);
    }

    class FirstListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ta.append("第一个事件监听器被触发，事件源是：" + e.getActionCommand() + "\n");
        }
    }

    class SecondListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            ta.append("单击了" + e.getActionCommand() + "按钮\n");
        }
    }

    public static void main(String[] args) {
        new MultiListener().init();
    }
}
